import {
  getAllDepartmentTree,
  getListEnterprise
} from '@/api/human/department'
import {
  getSysUserTypeCodeAllRole
} from '@/api/manage/role'

const state = {
  orgData: [],
  orgRoleData: [],
  enterpriseData: [],
  orgFields: { children: 'children', title: 'orgName', key: 'orgId', value: 'orgId' },
  accountTypeData: [
    { value: '1A52UPA32YJUJHW7MWEPACSODDIZXR', label: '人资管理端' },
    // {value:'1JX2GQKRMKOYMHFK4HSZQDM5FN917Y',label:'人资企业端'},
  ]
}
const mutations = {
  GET_ENTERPRISE_DATA: (state, data) => {
    state.enterpriseData = data
  },
  GET_ORG_DATA: (state, data) => {
    state.orgData = data
  },
  GET_ROLE_DATA: (state, data) => {
    state.orgRoleData = data
  }
}
const actions = {
  getAllEnterprise({ commit }) {
    return new Promise((resolve, reject) => {
      getListEnterprise().then(res => {
        if (res.code === '1') {
          let array = res.data;
          commit('GET_ENTERPRISE_DATA', array)
          resolve(array);
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  getAllDepartmentTree({ commit }, orgId) {
    return new Promise((resolve, reject) => {
      getAllDepartmentTree(orgId).then(res => {
        if (res.code === '1') {
          let array = JSON.parse(JSON.stringify(res.data).replace(/code/g, 'value').replace(/name/g, 'label'));
          commit('GET_ORG_DATA', array)
          resolve(array);
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  getAllDepartmentRole({ commit }, roleTypeCode) {
    return new Promise((resolve, reject) => {
      getSysUserTypeCodeAllRole(roleTypeCode).then(res => {
        if (res.code === '1') {
          let array = JSON.parse(JSON.stringify(res.data).replace(/code/g, 'value').replace(/name/g, 'label'));
          commit('GET_ROLE_DATA', array)
          resolve(array);
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

}
export default {
  namespaced: true,
  actions,
  state,
  mutations
}
